package app.qrcode.browser.todoapp.notification

import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import app.qrcode.browser.todoapp.App
import app.qrcode.browser.todoapp.App.Companion.appContext
import app.qrcode.browser.todoapp.MainActivity
import app.qrcode.browser.todoapp.R
import app.qrcode.browser.todoapp.data.ToDoDataBase
import app.qrcode.browser.todoapp.data.models.ToDoData
import app.qrcode.browser.todoapp.data.repository.ToDoRepository
import app.qrcode.browser.todoapp.data.viewmodel.ToDoViewModel
import app.qrcode.browser.todoapp.utils.dateFormater
import app.qrcode.browser.todoapp.utils.timeFormater
import kotlinx.coroutines.*
import kotlin.math.log

class NotificationService : Service() {


//     class GrayInnerService : Service() {
//         override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//             startForeground(1, Notification())
//             stopForeground(true)
//             stopSelf()
//             return super.onStartCommand(intent, flags, startId)
//         }
//
//         override fun onBind(p0: Intent?): IBinder? {
//             return null
//         }
//
//     }


    override fun onCreate() {
        super.onCreate()
        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel =
                NotificationChannel("my_service", "闹钟提醒", NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(channel)
        }
        val intent = Intent(this, MainActivity::class.java)
        val pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE)
        val notification = NotificationCompat.Builder(this, "my_service")
            .setContentTitle("这是提醒")
            .setContentText("This is content Title")
            .setSmallIcon(R.drawable.ic_baseline_add_24)
            .setContentIntent(pi)
            .build()
        startForeground(1, notification)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//        var nofityid = 1
        //这里写提醒事件
        CoroutineScope(context = Dispatchers.IO).launch {
            val toDoDao = ToDoDataBase.getDataBase(App.appContext).toDoDao()
//           val repository: ToDoRepository = ToDoRepository(toDoDao)

            val manager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channel = NotificationChannel(
                    "todo提醒",
                    "发送提醒",
                    NotificationManager.IMPORTANCE_LOW
                )
                manager.createNotificationChannel(channel)

            }
            while (true) {
                delay(10000)
                var systemDate = dateFormater.format(System.currentTimeMillis())
                var systemTime = timeFormater.format(System.currentTimeMillis())

                val chooseIsNoticedItem: List<ToDoData> = toDoDao.chooseIsNoticedItem()
                for (item in chooseIsNoticedItem) {
                    var itemTime = if (item.time == null) "06-00" else item.time
                    var itemDate = if (item.date == null) systemDate else item.date
                    if (itemTime == systemTime && itemDate == systemDate) {
                        Log.w("gyk", "NotificationService.onStartCommand -> 想等了")
                        val notication =
                            NotificationCompat.Builder(appContext, item.id.toString())
                                .setContentTitle(item.title)
                                .setContentText(item.descriptor)
                                .setSmallIcon(R.drawable.ic_baseline_notifications_24)
                                .setGroup("提醒组")
                                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                                .build()
                        manager.notify(item.id, notication)
                    }
                    Log.w(
                        "gyk",
                        "NotificationService.onStartCommand -> 日期 $itemDate 时间 $itemTime 系统日期 $systemDate 系统时间 $systemTime"
                    )
                }
            }
        }

        return super.onStartCommand(intent, flags, startId)
    }

    override fun onBind(intent: Intent): IBinder {
        TODO("Return the communication channel to the service.")
    }

}